{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json\n",
    "from aip import AipSpeech\n",
    "\n",
    "appId = '11063361'\n",
    "apiKey = 'S9XOU2PDMLo9cFh9LRdw08EU'\n",
    "secretKey = '31f2ca52d9b4ba558188cbd9de09b9d0'\n",
    "\n",
    "a = AipSpeech(appId, apiKey, secretKey)\n",
    "\n",
    "def get_token():\n",
    "    '''获取token'''\n",
    "    baidu_server = \"https://aip.baidubce.com/oauth/2.0/token?\" #获取token的地址\n",
    "    grant_type = \"client_credentials\" #固定值\n",
    "    client_id = 'S9XOU2PDMLo9cFh9LRdw08EU' #apiKEY\n",
    "    client_secret = '31f2ca52d9b4ba558188cbd9de09b9d0' #secrenKEY\n",
    "    #组合url\n",
    "    url = \"%sgrant_type=%s&client_id=%s&client_secret=%s\"%(baidu_server, grant_type, client_id, client_secret)\n",
    "    res = requests.post(url)\n",
    "    token = json.loads(res.text)[\"access_token\"]\n",
    "    print(token)\n",
    "    return token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "24.97a0e3414f2e8867175ccc95977fef8a.2592000.1525797900.282335-11063361\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'corpus_no': '6542119526760106919',\n",
       " 'err_msg': 'success.',\n",
       " 'err_no': 0,\n",
       " 'result': ['北京科技馆，'],\n",
       " 'sn': '175435425171523205900'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def get_file_content(filepath):\n",
    "    with open(filepath, 'rb') as fp:\n",
    "        return fp.read()\n",
    "token = get_token()\n",
    "file = r'D:\\myCode\\myPythonCode\\API\\baidu\\yuyin2wenzi\\public\\16k.pcm'\n",
    "speech = get_file_content(file)\n",
    "a.asr(speech, 'pcm', 16000, {'dev_pid':'1536', 'cuid':'yangleidat', 'token':token})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
